new Vue({
  el: '#container',
  data: {
    teachPlanTreeNodes: [],
    coursePubId: 0,
    currentTeachPlanId: 0,
    currentTeachplanName: '',
    mediaType: '001002', // 直播001001; 视频001002; 文档001003; 作业001004;
    teachplanMedia: {},
    playUrl: '',
    work: {},
    question: '',
    answer: ''
  },
  methods: {
    // 处理点击小节事件
    handle_click_tree_node(subTeachPlanTreeNode) {
	  console.info(subTeachPlanTreeNode);
      this.currentTeachPlanId = subTeachPlanTreeNode.teachPlanId
      this.currentTeachplanName = subTeachPlanTreeNode.pname
      
	  if( subTeachPlanTreeNode.mediaType){
		  this.mediaType = subTeachPlanTreeNode.mediaType;
	  }else{
		  this.mediaType = '001002';
	  }

      // 直播
      if (this.mediaType === '001001') {
        this.play_teachplan_media()
      }
      // 视频
      else if (this.mediaType === '001002') {
        let teachplanMedia = subTeachPlanTreeNode.teachplanMedia
        if (teachplanMedia) {
          this.teachplanMedia = teachplanMedia
          this.play_teachplan_media()
        } else {
          alert('当前小节没有绑定视频')
        }
      }
      // 作业
      else if (this.mediaType === '001004') {
        this.question = ''
        this.answer = ''

        let work = subTeachPlanTreeNode.work
        if (work) {
          this.work = work
          this.display_work()
        } else {
          alert('当前小节没有绑定作业')
        }
      } else {
        console.log('文档类型暂时不用考虑,测试的类型为：'+this.mediaType)
		console.info(subTeachPlanTreeNode);
      }
    },
    // 播放小节视频
    play_teachplan_media() {
      let params = {
        coursePubId: this.coursePubId,
        teachplanId: this.currentTeachPlanId
      }
      if (this.mediaType !== '001001') {
        params.mediaId = this.teachplanMedia.mediaId
      }

      // 调用接口获取视频播放地址
      get_resource_url(params).then(data => {
        this.playUrl = data.playUrl

        // 调用ckplayer播放视频
        this.play_ckplayer()
        // 保存课程学习进度
        this.save_records_progress()
      })
    },
    // 显示小节作业
    display_work() {
      get_teaching_work(this.work.workId).then(data => {
        this.question = data.question
      })
    },
    // 调用ckplayer播放视频
    play_ckplayer() {
      let videoObject = {}

      if (this.mediaType === '001001') {
        // rtmp协议视频播放示例：http://www.ckplayer.com/sampleX/rtmp.html
        videoObject = {
          container: '#vdplay', //“#”代表容器的ID，“.”或“”代表容器的class
          variable: 'player', //该属性必需设置，值等于下面的new chplayer()的对象
          autoplay: true, //自动播放
          live: true, //直播视频形式
          video: 'rtmp://' + this.playUrl //视频地址
        }
      } else {
        // 普通视频播放演示，兼容模式：http://www.ckplayer.com/sampleX/mp4.html
        videoObject = {
          container: '#vdplay', //“#”代表容器的ID，“.”或“”代表容器的class
          variable: 'player', //该属性必需设置，值等于下面的new chplayer()的对象
          // poster: 'pic/wdm.jpg', //封面图片
          video: this.playUrl //视频地址
        }
      }
      let player = new ckplayer(videoObject)
    },
    // 保存课程学习进度
    save_records_progress() {
      let data = {
        coursePubId: this.coursePubId,
        teachPlanId: this.currentTeachPlanId,
        teachPlanName: this.currentTeachplanName
      }

      save_records_progress(JSON.stringify(data)).then(data => {
        // console.log('保存学习进度：')
        // console.log(data)
      })
    },
    // 处理提交作业事件
    handle_submit_work_record() {
      if (!this.answer) {
        alert('请输入答案')
        return
      }
      let data = {
        coursePubId: this.coursePubId,
        teachplanId: this.currentTeachPlanId,
        teachplanName: this.currentTeachplanName,
        workId: this.work.workId,
        answer: this.answer
      }

      submit_work_record(JSON.stringify(data)).then(data => {
        alert('提交答案成功')
        // console.log(data)
      })
    }
  },
  created() {
    this.coursePubId = parseInt(localURL.searchParams.get('coursePubId'), 10)
    let teachPlanId = localURL.searchParams.get('teachplanId')
    if (teachPlanId) {
      this.currentTeachPlanId = parseInt(teachPlanId, 10)
    }

    search_course_index(this.coursePubId).then(data => {
      this.teachPlanTreeNodes = JSON.parse(data.teachplan).teachPlanTreeNodes
	  
      // 没有参数，点击默认小节
      if (!teachPlanId) {
		  console.info("没有指定小节")
        this.handle_click_tree_node(
          this.teachPlanTreeNodes[0].teachPlanTreeNodes[0]
        )
        return
      }

      // 有参数时，点击指定小节
      for (let i = 0; i < this.teachPlanTreeNodes.length; i++) {
		  
        let subTeachPlanTreeNodes = this.teachPlanTreeNodes[i]
          .teachPlanTreeNodes
        for (let j = 0; j < subTeachPlanTreeNodes.length; j++) {
          if (
            subTeachPlanTreeNodes[j].teachPlanId === this.currentTeachPlanId
          ) {
			  console.info("有指定小节")
            this.handle_click_tree_node(subTeachPlanTreeNodes[j])
            return
          }
        }
      }
    })

    my_course_record(this.coursePubId).then(data => {
      // console.log('当前学习进度：')
      // console.log(data)
    })
  }
})
